package com.jhd.fuzhou.util;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.jhd.fuzhou.view.login.FuzhouApplication;
import com.jhd.fuzhou.R;
import com.jhd.fuzhou.service.SupervisionService;
import com.jhd.fuzhou.view.task.TaskDetailActivity;
import com.jhd.fuzhou.vo.JsonForResult;
import com.jhd.fuzhou.vo.SupervisionInspectionVo;
import com.jhd.fuzhou.vo.TaskSearch;

import net.zigin.commons.util.StringUtil;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class GetSupervisionInspectionTaskSituation {
    public static void getSupervisionInspectionTaskSituation(FuzhouApplication application, Context context, RecyclerView recyclerView, TaskSearch taskSearch){
        Retrofit retrofit = UtilHelper.retrofit(application, UtilHelper.baseUrls.API());//URL的头
//        Retrofit retrofit = UtilHelper.retrofit(application,"http://192.168.1.96:9004/");//URL的头
        SupervisionService vs = retrofit.create(SupervisionService.class);
        Map<String, String> params = new LinkedHashMap<String, String>();
        params.put("isHomepage", taskSearch.getIsHomepage());
        if(!StringUtil.isNullOrEmpty(taskSearch.getStartTime()))params.put("startTime", taskSearch.getStartTime());
        if(!StringUtil.isNullOrEmpty(taskSearch.getEndTime()))params.put("endTime", taskSearch.getEndTime());
        if(!StringUtil.isNullOrEmpty(taskSearch.getTaskStatus()))params.put("taskStatus", taskSearch.getTaskStatus());
        if(!StringUtil.isNullOrEmpty(taskSearch.getUsername()))params.put("username", taskSearch.getUsername());
        if(!StringUtil.isNullOrEmpty(taskSearch.getTaskName()))params.put("taskName", taskSearch.getTaskName());
        if(!StringUtil.isNullOrEmpty(taskSearch.getIsFinish()))params.put("isFinish", taskSearch.getIsFinish());
        if(!("1".equals(application.getOrgLevel()))){
            if(application.getOrgId()!=null)params.put("superviseOrgId",application.getOrgId());
        }
        Call<JsonForResult> jsonForResultCall = vs.getSupervisionInspectionTaskSituation(application.getAccessToken(),params);//调用接口获取json返回
        jsonForResultCall.enqueue(new Callback<JsonForResult>() {
            @Override
            public void onResponse(Call<JsonForResult> call, Response<JsonForResult> response) {
                try {
                    JsonForResult jsonForResult = (JsonForResult) response.body();
                    if (jsonForResult != null && jsonForResult.isSuccess()) {
                        if(jsonForResult.getData()!=null){
                            List<SupervisionInspectionVo> supervisionInspectionVoList = JSON.parseArray(jsonForResult.getData().toString(), SupervisionInspectionVo.class);
                            LinearLayoutManager layoutManager = new LinearLayoutManager(context);
                            recyclerView.setLayoutManager(layoutManager);
                            TaskAdapter taskAdapter=new TaskAdapter(supervisionInspectionVoList,application,taskSearch,context);
                            recyclerView.setAdapter(taskAdapter);
                        }else{
                            List<SupervisionInspectionVo> supervisionInspectionVoList = new ArrayList<>();
                            LinearLayoutManager layoutManager = new LinearLayoutManager(context);
                            recyclerView.setLayoutManager(layoutManager);
                            TaskAdapter taskAdapter=new TaskAdapter(supervisionInspectionVoList,application,taskSearch,context);
                            recyclerView.setAdapter(taskAdapter);
                        }
                    } else {
                        UtilHelper.alertMsg(context, "错误", jsonForResult.getMessage());
                    }
                } catch (Exception e) {
                    CustomToast.showText(context,e.getLocalizedMessage(),3000,0,0).show();
                }
            }

            @Override
            public void onFailure(Call<JsonForResult> call, Throwable t) {
                String msg = "网络异常!";
                if (StringUtil.isNullOrEmpty(t.getMessage()) == false) msg = msg + t.getMessage();
                UtilHelper.alertMsg(context, "错误", msg);
            }
        });
    }

    public static class TaskAdapter extends RecyclerView.Adapter<TaskAdapter.ViewHolder> {
        private List<SupervisionInspectionVo> supervisionInspectionVoList;private FuzhouApplication application;private Context context;TaskSearch taskSearch;
        public TaskAdapter(List<SupervisionInspectionVo> supervisionInspectionVoList,FuzhouApplication application,TaskSearch taskSearch,Context context) {
            this.supervisionInspectionVoList=supervisionInspectionVoList;
            this.application=application;
            this.application=application;
            this.context=context;
            this.taskSearch=taskSearch;
        }

        @Override
        public TaskAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_sy, parent, false);
            TaskAdapter.ViewHolder viewHolder = new TaskAdapter.ViewHolder(itemView);
            return viewHolder;
        }

        @Override
        public void onBindViewHolder(@NonNull TaskAdapter.ViewHolder holder, int position) {
//            holder.tv_taskName.setText(StringShow.showStr(supervisionInspectionVoList.get(position).getTaskName())+supervisionInspectionVoList.get(position).getId());
            holder.tv_taskName.setText(StringShow.showStr(supervisionInspectionVoList.get(position).getTaskName()));
            if(StringUtil.isNullOrEmpty(supervisionInspectionVoList.get(position).getCreateTime())){
                holder.tv_date.setText("");
            }else{
                holder.tv_date.setText(supervisionInspectionVoList.get(position).getCreateTime().substring(0,11));
            }
//            holder.tv_dept.setText(StringShow.showStr(supervisionInspectionVoList.get(position).getSupervisedUnitName()));
            holder.tv_dept.setText(StringShow.showStr(supervisionInspectionVoList.get(position).getSuperviseOffice()));
            holder.tv_status.setText(StringShow.showStr(supervisionInspectionVoList.get(position).getApprovalStatus()));
            if("0".equals(supervisionInspectionVoList.get(position).getSupervisedType())){
                holder.tv_task_type.setText("日常监督");
            }else if("1".equals(supervisionInspectionVoList.get(position).getSupervisedType())){
                holder.tv_task_type.setText("专项监督");
            }else if("2".equals(supervisionInspectionVoList.get(position).getSupervisedType())){
                holder.tv_task_type.setText("国家双随机");
            }else if("3".equals(supervisionInspectionVoList.get(position).getSupervisedType())){
                holder.tv_task_type.setText("省级双随机");
            }
            holder.linearLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(context, TaskDetailActivity.class);
                    Gson gson = new Gson();
                    intent.putExtra("isFinish",taskSearch.getIsFinish());
                    intent.putExtra("data", gson.toJson(supervisionInspectionVoList.get(position)));
                    context.startActivity(intent);
                }
            });
        }

        @Override
        public int getItemCount() {
            return supervisionInspectionVoList.size();
        }

        class ViewHolder extends RecyclerView.ViewHolder {
            TextView tv_date,tv_taskName,tv_dept,tv_task_type,tv_status;
            LinearLayout linearLayout;
            public ViewHolder(View itemView) {
                super(itemView);
                this.tv_date = itemView.findViewById(R.id.tv_date);
                this.tv_taskName = itemView.findViewById(R.id.tv_taskName);
                this.tv_dept = itemView.findViewById(R.id.tv_dept);
                this.tv_task_type = itemView.findViewById(R.id.tv_task_type);
                this.tv_status = itemView.findViewById(R.id.tv_status);
                this.linearLayout = itemView.findViewById(R.id.linearLayout);
            }
        }
    }
}
